//
//  ZCQuickLoginView.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/26.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCQuickLoginView.h"
#import "ZCQuickLoginModel.h"
#import "ZCVerticalButton.h"


typedef enum : NSUInteger {
    QQButton = 0,
    SinnaButton = 1,
    TensentButton = 2,
} BtnType;

@interface ZCQuickLoginView ()
@property (strong, nonatomic) NSArray *appArray;
@end


@implementation ZCQuickLoginView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(NSArray *)appArray{
    if (_appArray == nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"QuickLoginMessage" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        
        _appArray = [ZCQuickLoginModel mj_objectArrayWithKeyValuesArray:array];
    }
    return _appArray;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return  self;
    
}

-(void)setupUI{
    
    UILabel *titleLab = [UILabel labelWithText:@"快速登录" textColor:[UIColor whiteColor] fontSize:15 textAlignment:NSTextAlignmentCenter];
    
    UIButton *leftImage = [UIButton buttonWithBackgroundImage:@"login_register_left_line" selectedImage:nil title:nil fontSize:0 tintColor:nil action:nil target:nil cornerRadius:0 highlighted:NO];
    leftImage.size = leftImage.currentImage.size;
    
    UIButton *rightImage = [UIButton buttonWithBackgroundImage:@"login_register_right_line" selectedImage:nil title:nil fontSize:0 tintColor:nil action:nil target:nil cornerRadius:0 highlighted:NO];
    rightImage.size = leftImage.currentImage.size;
    
    
    [self addSubview:titleLab];
    [self addSubview:leftImage];
    [self addSubview:rightImage];
    
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self);
    }];
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLab);
        make.trailing.equalTo(titleLab.mas_leading);
    }];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLab);
        make.leading.equalTo(titleLab.mas_trailing);
    }];
   
    
    CGFloat width = 80;
    CGFloat margin = ([UIScreen mainScreen].bounds.size.width- self.appArray.count * width)/self.appArray.count;
    NSInteger i = 0;
    
    for (ZCQuickLoginModel*model in self.appArray) {
        
        UIButton *button = [UIButton buttonWithImage:model.AppIconNomal highlightedImage:model.AppIconHighlighted];
        
        UILabel *label = [UILabel labelWithText:model.AppName textColor:[UIColor whiteColor] fontSize:12 textAlignment:NSTextAlignmentCenter];
        
        button.frame = CGRectMake(margin/2 + i*(margin+width), 50, width, width);
        label.frame = CGRectMake(button.x, button.y+button.width, width, 20);
        button.tag = i++;

        
        [self addSubview:button];
        [self addSubview:label];
        
    }
    
}
/**
 *  快速登录按钮点击事件
 *
 *  @param sender 按钮本身
 */
-(void)buttonAction:(UIButton *)sender{
    
    [self endEditing:YES];
    if (sender.tag == QQButton) {
        
        ZCLog(@"QQ登录");
        
    }else if(sender.tag == SinnaButton){
        
        ZCLog(@"微博登录");
        
    }else if (sender.tag == TensentButton){

        ZCLog(@"腾讯微博");
    }
}

@end
